#!/bin/bash
 
netcardname=$1
echo "$netcardname"
# 获取所有网卡的名称
if [ -z "$netcardname" ]; then
    		echo "设置网卡优先级时候，需要指定网卡名称"
			exit 0;
else
	ip_address=$(ip addr show $netcardname | grep "inet " | awk '{print $2}' | cut -d/ -f1)
    # 检查IP地址是否为空
    if [ -z "$ip_address" ]; then
    		echo "设置制定网卡优先级时， 网卡$1 的IP地址不能为空"
			exit 0
	else
    		echo "网卡 $netcardname 的IP地址是: $ip_address  metric 0"
			/usr/sbin/ifmetric $netcardname 0
	fi
fi



net_devices=$(ip link show | awk -F': ' '{print $2}' | grep -v "lo\|${netcardname}")
# 打印网卡列表
echo "Available network interfaces: " 
#判断 $1

metric_value=1
for dev in $net_devices; do
    other_address=$(ip addr show $dev | grep "inet " | awk '{print $2}' | cut -d/ -f1)
    # 检查IP地址是否为空
	metric_value=$(expr $metric_value + 1)

    if [ -z "$other_address" ]; then
    		echo "网卡 $dev 的IP地址为空"
	else
    		echo "网卡 $dev 的IP地址是: $other_address  $metric_value"
			/usr/sbin/ifmetric $dev $metric_value
	fi
done
